<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox-button
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox-button>
    </el-checkbox-group>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="submitHandler">确定</el-button>
      <el-button @click="closeHandler">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  name: 'VueAdminTemplateMasterSetrole',

  data() {
    return {
      list: [],
      roleIds: [],
      id: ''
    }
  },

  mounted() {

  },
  created() {
    this.updateRoles()
  },
  methods: {
    async updateRoles() {
      const res = await getRoles()
      //   console.log(res)
      if (res.code !== 10000) return
      this.list = res.data.rows
      this.updateEmployeeById()
    },
    async submitHandler() {
      const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
      //   console.log(res)
      if (res.code !== 10000) return
      console.log()
      this.$parent.$parent.roleShowDialog = false
    //   this.$parent.$parent.loadEmp()
    },
    closeHandler() {
      this.$parent.$parent.roleShowDialog = false
    },
    async updateEmployeeById() {
      const res = await getUserDetailById(this.id)
      //   console.log(res)
      if (res.code !== 10000) return
      this.roleIds = res.data.roleIds
    }
  }
}
</script>

<style lang="scss" scoped></style>
